//
//  BuyRecordViewController.m
//  YunGou
//
//  Created by macbook on 16/5/19.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "BuyRecordViewController.h"
#import "BuyDetailViewController.h"
#import "GoodsDetailViewController.h"
#import "NoMoreDataView.h"
#import "BuyRecordCell1.h"
#import "BuyRecordCell2.h"
#import "ProductModel.h"
#import "NullDataView.h"
#import <MJRefresh.h>

@interface BuyRecordViewController ()
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong) NullDataView *nullDataView;
@end

@implementation BuyRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
}

- (void)loadData{
    _dataArray = [NSMutableArray array];
    [MBProgressHUD showMessage:@"正在加载"];
    [NetWorkTool buyRecordWithSuccess:^(id object) {
        [MBProgressHUD hideHUD];
        if ([object[@"code"] integerValue] == 1) {
            for (NSDictionary *dict in object[@"data"]) {
                ProductModel *model = [ProductModel modelWithDict:dict];
                [_dataArray addObject:model];
            }
        }
        
        if (_dataArray.count == 0) {
            [self.view addSubview:self.nullDataView];
            self.tableView.tableFooterView = nil;
        }else{
            if (_nullDataView) {
                [_nullDataView removeFromSuperview];
                _nullDataView = nil;
            }
            NoMoreDataView *footerView = [[NoMoreDataView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
            self.tableView.tableFooterView = footerView;
        }

        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma -mark tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *baseCell = nil;
    ProductModel *model = _dataArray[indexPath.row];
    
    
#pragma -mark 待修改
    
    if (NowTime > model.q_end_time) {
       BuyRecordCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.model = model;
        cell.buyRecordBlock = ^(NSInteger tag){
            if (tag == 2) {
                BuyDetailViewController *buyDetailVC = SB(@"Mine", @"BuyDetailVC");
                buyDetailVC.productModel = _dataArray[indexPath.row];
                [self.navigationController pushViewController:buyDetailVC animated:YES];
            }else{
                [NetWorkTool addShopCartWithGoodsId:model.Id success:^(id object) {
                    if ([object[@"code"] integerValue] == 1) {
                        self.tabBarController.selectedIndex = 3;
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                } failure:^(NSError *error) {
                    
                }];
            }
        };
        
        cell.imageBlock = ^(ProductModel *model){
            GoodsDetailViewController *goodsDetailVC = SB(@"Common", @"GoodsDetailVC");
            goodsDetailVC.detailType = DonotAnnounce;

            goodsDetailVC.productModel = model;
            [self.navigationController pushViewController:goodsDetailVC animated:YES];
        };
        
        baseCell = cell;
    }else{
        BuyRecordCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.model = model;
        cell.buyRecordBlock = ^{
            BuyDetailViewController *buyDetailVC = SB(@"Mine", @"BuyDetailVC");
            buyDetailVC.productModel = _dataArray[indexPath.row];
            [self.navigationController pushViewController:buyDetailVC animated:YES];
        };
        
        cell.imageClickBlock = ^(ProductModel *model){
            GoodsDetailViewController *goodsDetailVC = SB(@"Common", @"GoodsDetailVC");
            goodsDetailVC.detailType = HasAnnounce;
            goodsDetailVC.productModel = model;
            [self.navigationController pushViewController:goodsDetailVC animated:YES];
        };
        baseCell = cell;
    }
    return baseCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDate *date = [NSDate date];
    double nowTime = [date timeIntervalSince1970];
    ProductModel *model = _dataArray[indexPath.row];
    if (nowTime < model.q_end_time) {
        return 125;
    }return 145;
}

#pragma -mark 懒加载
- (NullDataView *)nullDataView{
    if (!_nullDataView) {
        _nullDataView = [NullDataView nullDataView];
        _nullDataView.bounds = self.tableView.bounds;
    }
    return _nullDataView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
